package start.spring.basic.report.service.filltemplete.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import start.spring.basic.constant.CommonConstant;
import start.spring.basic.page.PageQuery;
import start.spring.basic.page.PageResult;
import start.spring.basic.report.dao.ReportTempleteDesignMapper;
import start.spring.basic.report.entity.ReportTempleteDesign;
import start.spring.basic.report.service.BaseServiceImpl;
import start.spring.basic.report.service.filltemplete.IReportTempleteDesignService;
import start.spring.basic.response.ResponseData;
import start.spring.basic.util.common.StringUtil;

/**
 * 模板设计Service业务层处理
 * 
 * @author xyy
 * @date 2023-12-27
 */
@Service
public class ReportTempleteDesignServiceImpl extends BaseServiceImpl implements IReportTempleteDesignService {
    
    @Autowired
    private ReportTempleteDesignMapper reportTempleteDesignMapper;

    /**
     * 查询模板设计列表
     * 
     * @param reportTempleteDesign 模板设计
     * @return 模板设计
     */
    @Override
    public PageResult<ReportTempleteDesign> selectReportTempleteDesignPageList(PageQuery pageQuery,ReportTempleteDesign reportTempleteDesign) {
        IPage<ReportTempleteDesign> page = super.getPage(pageQuery);
        QueryWrapper<ReportTempleteDesign> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(reportTempleteDesign.getTempleteId()!=null, ReportTempleteDesign::getTempleteId, reportTempleteDesign.getTempleteId());
        queryWrapper.lambda().like(!StringUtil.isEmpty(reportTempleteDesign.getTempleteName()), ReportTempleteDesign::getTempleteName, reportTempleteDesign.getTempleteName());
        queryWrapper.lambda().eq(!StringUtil.isEmpty(reportTempleteDesign.getReleaseFlag()), ReportTempleteDesign::getReleaseFlag, reportTempleteDesign.getReleaseFlag());
        if(!StringUtil.isEmpty(reportTempleteDesign.getBeginCreateTime()) && !StringUtil.isEmpty(reportTempleteDesign.getEndCreateTime())) {
        	queryWrapper.lambda().between(ReportTempleteDesign::getCreateTime, reportTempleteDesign.getBeginCreateTime() + CommonConstant.SURFIX_DATE_BEGIN, reportTempleteDesign.getEndCreateTime() + CommonConstant.SURFIX_DATE_END);
        }
        queryWrapper.lambda().orderByDesc(ReportTempleteDesign::getUpdateTime);
        page = this.reportTempleteDesignMapper.selectPage(page, queryWrapper);
        return super.getPageResult(page);
    }
    
    /**
     * 查询模板设计详情
     * 
     * @param reportTempleteDesign 模板设计
     * @return 模板设计
     */
    @Override
    public List<ReportTempleteDesign> selectReportTempleteDesignList(ReportTempleteDesign reportTempleteDesign) {
        QueryWrapper<ReportTempleteDesign> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(reportTempleteDesign.getTempleteId()!=null, ReportTempleteDesign::getTempleteId, reportTempleteDesign.getTempleteId());
        queryWrapper.lambda().like(!StringUtil.isEmpty(reportTempleteDesign.getTempleteName()), ReportTempleteDesign::getTempleteName, reportTempleteDesign.getTempleteName());
        queryWrapper.lambda().eq(!StringUtil.isEmpty(reportTempleteDesign.getReleaseFlag()), ReportTempleteDesign::getReleaseFlag, reportTempleteDesign.getReleaseFlag());
        if(!StringUtil.isEmpty(reportTempleteDesign.getBeginCreateTime()) && !StringUtil.isEmpty(reportTempleteDesign.getEndCreateTime())) {
        	queryWrapper.lambda().between(ReportTempleteDesign::getCreateTime, reportTempleteDesign.getBeginCreateTime() + CommonConstant.SURFIX_DATE_BEGIN, reportTempleteDesign.getEndCreateTime() + CommonConstant.SURFIX_DATE_END);
        }
        return this.reportTempleteDesignMapper.selectList(queryWrapper);
    }
    
    /**
     * 查询可填报列表
     * 当前未设计人员权限控制
     * @return
     * @since           1.0
     */
    @Override
	public ResponseData<List<ReportTempleteDesign>> selectCanfillTempleteList() {
    	QueryWrapper<ReportTempleteDesign> queryWrapper = new QueryWrapper<>();
    	queryWrapper.lambda().eq(ReportTempleteDesign::getReleaseFlag, "Y");
		return ResponseData.successData(this.reportTempleteDesignMapper.selectList(queryWrapper));
	}

    /**
     * 新增模板设计
     * 
     * @param reportTempleteDesign 模板设计
     * @return 结果
     */
    @Override
    public ResponseData<String> insertReportTempleteDesign(ReportTempleteDesign reportTempleteDesign) {
    	if(reportTempleteDesign==null||StringUtil.isEmpty(reportTempleteDesign.getTempleteName())) {
    		return ResponseData.errorMsg("模板名称不可为空，请填写!");
    	}else {
    		PageQuery pageQuery = new PageQuery();
    		PageResult<ReportTempleteDesign> reportTempleteDesigns = selectReportTempleteDesignPageList(pageQuery,reportTempleteDesign);
    		if(reportTempleteDesigns!=null&&reportTempleteDesigns.getTotal()>0) {
    			return ResponseData.errorMsg("模板名称已存在，请修改后重新提交!");
    		}else {
    			reportTempleteDesign.setUserId(super.getSysUser().getUserId());
    			reportTempleteDesign.setUserName(super.getSysUser().getUserName());
    			reportTempleteDesign.setNickName(super.getSysUser().getNickName());
    			this.reportTempleteDesignMapper.insert(reportTempleteDesign);
    			return ResponseData.successMsg("模板已保存！");
    		}
    	}
    }

    /**
     * 修改模板设计
     * 
     * @param reportTempleteDesign 模板设计
     * @return 结果
     */
    @Override
    public ResponseData<String> updateReportTempleteDesign(ReportTempleteDesign reportTempleteDesign) {
    	if(reportTempleteDesign!=null&&reportTempleteDesign.getTempleteId()!=null) {
    		UpdateWrapper<ReportTempleteDesign> updateWrapper = new UpdateWrapper<>();
        	if(!super.getSysUser().isAdmin()) {
        		//非管理员只能修改自己创建的模板
        		updateWrapper.lambda().eq(ReportTempleteDesign::getUserId, super.getSysUser().getUserId());
        	}
        	updateWrapper.lambda().eq(ReportTempleteDesign::getTempleteId, reportTempleteDesign.getTempleteId());
        	
            this.reportTempleteDesignMapper.update(reportTempleteDesign, updateWrapper);
            return ResponseData.successMsg("模板已更新！");
    	}else {
    		return ResponseData.errorMsg("未检测到需要更新的模板，请指定！");
    	}
    }

    /**
     * 批量删除模板设计
     * 
     * @param templeteIds 需要删除的模板设计主键
     * @return 结果
     */
    @Override
    public ResponseData<String> deleteReportTempleteDesignByTempleteIds(List<Long> templeteIds) {
    	if(templeteIds!=null&&templeteIds.size()>0) {
    		QueryWrapper<ReportTempleteDesign> queryWrapper = new QueryWrapper<>();
        	if(!super.getSysUser().isAdmin()) {
        		//非管理员只能删除自己创建的模板
        		queryWrapper.lambda().eq(ReportTempleteDesign::getUserId, super.getSysUser().getUserId());
        	}
        	queryWrapper.lambda().in(ReportTempleteDesign::getTempleteId, templeteIds);
            
        	this.reportTempleteDesignMapper.deleteBatchIds(templeteIds);
        	
        	return ResponseData.successMsg("模板已删除！");
    	}else {
    		return ResponseData.errorMsg("请指定需要删除的模板！");
    	}
    }
}
